package com.moonypoony.superactionrpg;

import java.nio.FloatBuffer;
import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

public class enemy {

	private int scrollx;
	private int scrolly;
	private int offsetx;
	private int offsety;
	static int map[][];
	private int direction;
	private boolean stop;
	
	public enemy(int x, int y) {
		this.scrollx = x;
		this.scrolly = y;
		this.offsetx = 0;
		this.offsety = 0;
		this.direction= 0;
		this.stop = false;
	}
	
	public boolean collision(int targetx, int targety) {
		boolean free = true;
		if(targetx==scrollx && targety==scrolly) {
			free = false;
		}
		if(offsetx>0){
			if(targetx==scrollx+1 && targety==scrolly) {
				free = false;
			}
		}
		if(offsetx<0) {
			if(targetx==scrollx-1 && targety==scrolly) {
				free = false;
			}
		}
		if(offsety>0) {
			if(targetx==scrollx && targety==scrolly+1) {
				free =  false;
			}
		}
		if(offsety<0) {
			if(targetx==scrollx && targety==scrolly-1) {
				free = false;
			}
		}
		return free;
	}
	
	static void setRoute(int[][] collision, int targetx, int targety, int offx, int offy) {
		
		boolean done = false;
		int amounttocheck= 1;
		int counter = 1;
		ArrayList<Integer> checkx = new ArrayList<Integer>(1);
		ArrayList<Integer> checky = new ArrayList<Integer>(1);
		
		checkx.add(targetx);
		checky.add(targety);
		
		if(offx>0) {
			checkx.add(targetx+1);
			checky.add(targety);
			map[targety][targetx+1] = -1;
		}
		if(offx<0) {
			checkx.add(targetx-1);
			checky.add(targety);
			map[targety][targetx-1] = -1;
		}
		if(offy>0) {
			checkx.add(targetx);
			checky.add(targety+1);
			map[targety+1][targetx] = -1;
		}
		if(offy<0) {
			checkx.add(targetx);
			checky.add(targety-1);
			map[targety-1][targetx] = -1;
		}
		
		for(int i=0; i<40; i++) {
			for(int j=0; j<40; j++) {
				if(collision[i][j]==0) {
					map[i][j]=0;
				} else {
					map[i][j]=-1;
				}
			}
		}
				
		
		while(!done)
		{
			
			amounttocheck = checkx.size();
			for(int i=0;i<amounttocheck;i++)
			{
				int x = checkx.get(i);
				int y = checky.get(i);
				map[y][x] = counter;
				
				if (x-1 >= 0) {
					if (map[y][x-1] == 0) {
						checkx.add(x-1); checky.add(y);
						map[y][x-1] = -1;
					}
				}
				if (x+1 <= 39) {
					if (map[y][x+1] == 0) {
						checkx.add(x+1); checky.add(y);
						map[y][x+1] = -1;
					}
				}	
				if (y-1 >= 0) {
					if (map[y-1][x] == 0) {
						checkx.add(x); checky.add(y-1);
						map[y-1][x] = -1;
					}
				}
				if (y+1 <= 39) {
					if (map[y+1][x] == 0) {
						checkx.add(x); checky.add(y+1);
						map[y+1][x] = -1;
					}
				}
				/*
				if(x == scrollx && y == scrolly) {
					done = true;
				}*/
			}
			counter++;
			checkx.subList(0,amounttocheck).clear();
			checky.subList(0,amounttocheck).clear();
			checkx.trimToSize();checky.trimToSize();
			if (checkx.isEmpty()) {
				done = true;
			}
			
		}
	
	}
	
	public void mapGenerated() {
		this.stop = false;
		if(offsetx==0 && offsety==0) {
			nextDirection();
		}
	}
	
	
	public void updatePosition() {
			if(!stop) {
			switch(direction) {
			case 0:
				offsety+=1;
				break;
			case 1:
				offsetx-=1;
				break;
				
			case 2:
				offsety-=1;
				break;
				
			case 3:
				offsetx+=1;
				break;
			}
			if(offsetx<=-32 && scrollx>=1) {
				offsetx=0;
				scrollx--;
				nextDirection();
			}
			else if(offsetx>=32 && scrollx<=38) {
				offsetx=0;
				scrollx++;
				nextDirection();
			}
			else if(offsety<=-32 && scrolly>=1) {
				offsety=0;
				scrolly--;
				nextDirection();
			}
			else if(offsety>=32 && scrolly<=38) {
				offsety=0;
				scrolly++;
				nextDirection();
			}
		}
	}

	private void nextDirection() {
		if(map[scrolly][scrollx]==2) {
			stop = true;
		} else {
			if(scrollx-1>=0) {
				if(map[scrolly][scrollx-1]==map[scrolly][scrollx]-1) {
					direction=1;
				}
					
			}
			if(scrollx+1<=39) {
				if(map[scrolly][scrollx+1]==map[scrolly][scrollx]-1) {
					direction=3;
				}
				
			}
			if(scrolly-1>=0) {
				if(map[scrolly-1][scrollx]==map[scrolly][scrollx]-1) {
					direction=2;
				}
				
			}
			if(scrolly+1<=39) {
				if(map[scrolly+1][scrollx]==map[scrolly][scrollx]-1) {
					direction=0;
				}
				
			}
		}
	}
	public void draw(GL10 gl, FloatBuffer coords, float posx, float posy, float offx, float offy, int[] texture, int frame) {
		if(scrollx>= posx && scrollx<posx+15 && scrolly>= posy && scrolly<posy+15) {
		
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glEnable(GL10.GL_TEXTURE_2D);
			gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
			if(stop) {
				gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[direction*2]);
			} else {
				gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[direction*2+frame]);
			}
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, coords);
			gl.glLoadIdentity();
			gl.glTranslatef(-0.6667f+((scrollx-posx)*32f-offx+offsetx)/400f, 1f-((scrolly-posy)*32f-offy+offsety)/240f, 0.0f);
			gl.glScalef(32/400f, 32/240f, 1.0f);
			gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
			
	
			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glDisable(GL10.GL_TEXTURE_2D);
		}
		
	}
}
